package com.bo.gather.rule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bo.gather.bean.Lengthinfo;
import com.bo.gather.bean.RemoveInfo;
import com.bo.gather.bean.ReplaceInfo;
import com.bo.gather.bean.weibo.IncludeNotFileInfo;
import com.bo.gather.bean.weibo.OrderLengthinfo;
import com.bo.gather.bean.weibo.WeiboBean;
import com.bo.gather.bean.weibo.WeiboRuleBean;

public class WeiboRule extends BaseRule{
	/**
	 * 计算父节点内容
	 * @param parentstr
	 * @param gatherbean
	 */
	public WeiboRuleBean calculateWeiboNote(String weibostr){
		//处理微博信息
		String []weiboarray=weibostr.split("\r\n");
		Map<String,String> weibomap=new HashMap<String,String>();
		for(String weiborule:weiboarray){
			if(!weiborule.trim().equals("")){
				int dengyu=weiborule.indexOf("=");
				String key=weiborule.substring(0, dengyu);
				String value=weiborule.substring(dengyu+1, weiborule.length());
				weibomap.put(key, value);
			}
		}
		if(weibomap.size()>0){
			WeiboRuleBean rule=new WeiboRuleBean();
			rule.setTitleBean(this.getWeiboBean(weibomap.get(BaseRule.weibotitle),weibomap.get(BaseRule.weibotitlerule)));
			rule.setContentBean(this.getWeiboBean(weibomap.get(BaseRule.weibocontent),weibomap.get(BaseRule.weibocontentrule)));
			rule.setUrl(weibomap.get(BaseRule.weibourl));
			rule.setImageUrl(weibomap.get(BaseRule.weiboimage));
			String users=weibomap.get(BaseRule.weibouser);
			rule.setUserArray(users.split(";"));
			rule.setUserRule(weibomap.get(BaseRule.weibouserrule));
			String []arraystr=weibomap.get(BaseRule.weiborandom).split("-");
			int [] arrayint=new int[arraystr.length];
			for(int i=0;i<arraystr.length;i++){
				arrayint[i]=Integer.parseInt(arraystr[i]);
			}
			rule.setRandom(arrayint);
			rule.setRandomAll(Integer.parseInt(weibomap.get(BaseRule.weiborandomall)));
			rule.setIsswitch(weibomap.get(BaseRule.weiboswitch));
			return rule;
		}
		return null;
	}
	
	/**
	 * 有 A,B 2个字段，weibo.B.rule=includefile:A 代表 B不能包含在中
	 * {includenotfile:title;clean:[0-9]clean:[a-y]clean:[A-Y]clean:\\pP|\\pSclean:" "}
	 * 
	 */
	public IncludeNotFileInfo calculateIncludeNotFile(String s){
		int lengthindex=s.indexOf("includenotfile:");
		if(lengthindex!=-1){
			IncludeNotFileInfo includeFileInfo=new IncludeNotFileInfo();
			String numstr=s.substring("includenotfile:".length(), s.indexOf(";"));
//			String []array=numstr.split("-");
			includeFileInfo.setTargetFile(numstr);
			String cleandel=s.substring(s.indexOf(";")+1,s.length());
//			String []deletearray=cleandel.split("@@");
//			lengthinfo.setIsDetele(deletearray[1]);
//			
			String []cleanarray=cleandel.split("clean:");
			if(cleanarray.length>0){
				includeFileInfo.setCleanList(new ArrayList<String>());
				for(String clean:cleanarray){
					if(!clean.equals("")){
						includeFileInfo.getCleanList().add(clean);
					}
				}
			}
			return includeFileInfo;
		}
		return null;
	}
	
	/**
	 * 计算其他名称的长度
	 * {order-length:content>100;clean:[0-9]clean:[a-y]clean:[A-Y]clean:\\pP|\\pSclean:" "}内容大于100
	 * @param s
	 * @return
	 */
	public OrderLengthinfo calculateOrderLength(String s){
		//验证长度
		int lengthindex=s.indexOf("length-order:");
		if(lengthindex!=-1){
			OrderLengthinfo lengthinfo=new OrderLengthinfo();
			String numstr=s.substring("length-order:".length(), s.indexOf(";"));
			if(numstr.indexOf(">=")!=-1){
				lengthinfo.setIfinfo(">=");
				lengthinfo.setLength(Integer.parseInt(numstr.substring(numstr.indexOf(">=")+2, numstr.length())));
				lengthinfo.setName(numstr.substring(0,numstr.indexOf(">=")));
			}else if(numstr.indexOf("<=")!=-1){
				lengthinfo.setIfinfo("<=");
				lengthinfo.setLength(Integer.parseInt(numstr.substring(numstr.indexOf("<=")+2, numstr.length())));
				lengthinfo.setName(numstr.substring(0,numstr.indexOf("<=")));
			}else if(numstr.indexOf(">")!=-1){
				lengthinfo.setIfinfo(">");
				lengthinfo.setLength(Integer.parseInt(numstr.substring(numstr.indexOf(">")+1, numstr.length())));
				lengthinfo.setName(numstr.substring(0,numstr.indexOf(">")));
			}else if(numstr.indexOf("<")!=-1){
				lengthinfo.setIfinfo("<");
				lengthinfo.setLength(Integer.parseInt(numstr.substring(numstr.indexOf("<"), numstr.length())));
				lengthinfo.setName(numstr.substring(0,numstr.indexOf("<")));
			}
			String cleandel=s.substring(s.indexOf(";")+1,s.length());
//			String []deletearray=cleandel.split("@@");
//			lengthinfo.setIsDetele(deletearray[1]);
//			
			String []cleanarray=cleandel.split("clean:");
			if(cleanarray.length>0){
				lengthinfo.setCleanList(new ArrayList<String>());
				for(String clean:cleanarray){
					if(!clean.equals("")){
						lengthinfo.getCleanList().add(clean);
					}
				}
			}
			return lengthinfo;
		}
		return null;
	}
	
	
	public WeiboBean getWeiboBean(String name,String rule){
		WeiboBean weibobean=new WeiboBean();
		weibobean.setName(name);
		if(rule!=null){
			String []rulearray=rule.replaceAll("≤", "").split("≥");
			for(String s:rulearray){
				//替换
				ReplaceInfo replaceInfo=this.calculateReplace(s);
				if(replaceInfo!=null){
					if(weibobean.getReplaceList()==null){
						List<ReplaceInfo> replacelist=new ArrayList<ReplaceInfo>();
						replacelist.add(replaceInfo);
						weibobean.setReplaceList(replacelist);
					}else{
						weibobean.getReplaceList().add(replaceInfo);
					}
					continue;
				}
				//删除操作
				RemoveInfo removeInfo=this.calculateRemove(s);
				if(removeInfo!=null){
					if(weibobean.getRemoveList()==null){
						List<RemoveInfo> removelist=new ArrayList<RemoveInfo>();
						removelist.add(removeInfo);
						weibobean.setRemoveList(removelist);
					}else{
						weibobean.getRemoveList().add(removeInfo);
					}
					continue;
				}
				//计算长度
				Lengthinfo lengthinfo=this.calculateLength(s);
				if(lengthinfo!=null){
					weibobean.setLengthinfo(lengthinfo);
				}
				
				//计算其他名称的长度
				OrderLengthinfo orderLengthinfo=this.calculateOrderLength(s);
				if(orderLengthinfo!=null){
					weibobean.setOrderLengthinfo(orderLengthinfo);
				}
				
				IncludeNotFileInfo includeNotFileInfo=this.calculateIncludeNotFile(s);
				if(includeNotFileInfo!=null){
					weibobean.setIncludeNotFileInfo(includeNotFileInfo);
				}
				
			}
		}
		return weibobean;
	}
}
